<link rel="stylesheet" type="text/css" href="/static/admin/plugins/bootstrap-fileinput/css/fileinput.min.css">
<link rel="stylesheet" type="text/css" href="/static/admin/plugins/bootstrap-select/dist/css/bootstrap-select.min.css">
<div class="page-header clearfix">
    <div class="pull-left">
        <h4 class="mt-0 mb-5">商品添加</h4>
        <ol class="breadcrumb mb-0">
            <li><a href="javascript:;">你可以对商品进行添加操作</a></li>
        </ol>
    </div>
</div>
<div class="main-content container-fluid">
    <div class="widget">
        <div class="widget-heading">
            <h3 class="widget-title">添加商品</h3>
        </div>
        <div class="widget-body">
            <form id="form-vertical" method="post" action="{:url('admin/shop/add')}" novalidate="novalidate">
                <div class="form-group">
                    <label>商品标题</label>
                    <input type="text" name="title" placeholder="请输入标题" data-messages="请输入标题" data-rule-required="true" class="form-control">
                </div>
                <div class="form-group">
                    <label>商品封面</label>
                    <input type="file" id="uploadImg" name="file" language="zh" >
                </div>
                <div class="row">
                    <div class="form-group col-md-6">
                        <label>商品原价</label>
                        <input type="text" name="price" placeholder="商品原价" data-rule-required="true" class="form-control">
                    </div>
                    <div class="form-group col-md-6">
                        <label>实际支付价格</label>
                        <input type="text" name="pay_price" placeholder="实际支付价格" data-rule-required="true" class="form-control">
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-md-6">
                        <label>商品分类</label>
                        <select style="display: block" multiple class="selectpicker" name="type_id" id="type_id">
                            {foreach $type as $val}
                            <option value="{$val['id']}">{$val['name']}</option>
                            {/foreach}
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label>商品标签</label>
                        <select multiple class="selectpicker" name="flag_id" id="flag_id">
                            {foreach $flag as $val}
                            <option value="{$val['id']}">{$val['name']}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label>商品描述</label>
                    <textarea name="description" class="form-control" id="" cols="30" rows="5"></textarea>
                </div>
                <div class="form-group">
                    <label>商品内容</label>
                    <script style="height:500px;" id="containers" name="content" type="text/plain"></script>
                </div>
                <div class="form-group">
                    <label>购买链接</label>
                    <input type="text" name="go_url" placeholder="请输入购买链接" data-rule-required="true" class="form-control">
                </div>
                <div class="form-group">
                    <label>优惠券链接</label>
                    <input type="text" name="coupon_url" placeholder="请输入优惠券链接" data-rule-required="true" class="form-control">
                </div>
                <div class="form-group">
                    <label>当前状态</label>
                    <select name="row_status" class="form-control">
                        <option value="">请选择</option>
                        <option value="0">正常</option>
                        <option value="-1">删除</option>
                    </select>
                </div>
                <input type="hidden" name="thumb" id="thumb">
                <input type="hidden" name="type" value="save">
                <input type="hidden" name="user_id" value="0">
                <button type="submit" name="btnSubmit" class="btn btn-raised btn-black btn-block">添加商品</button>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript" src="/static/admin/plugins/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js"></script>
<script type="text/javascript" src="/static/admin/plugins/bootstrap-fileinput/js/fileinput.min.js"></script>
<script type="text/javascript" src="/static/admin/plugins/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
<script type="text/javascript" src="/static/admin/plugins/bootstrap-fileinput/js/zh.js"></script>
<script type="text/javascript" src="/static/admin/plugins/jquery-validation/dist/jquery.validate.min.js"></script>
<script type="text/javascript" src="/static/admin/plugins/jquery-validation/dist/additional-methods.min.js"></script>
<script src="/static/admin/plugins/bdeditor/ueditor.config.js"></script>
<script src="/static/admin/plugins/bdeditor/ueditor.all.js"></script>
<script>
    var ue = UE.getEditor('containers');
    var token = '{$qiniu_token}';
    $(function () {

        $("#uploadImg").fileinput({
            language:'zh',
            uploadUrl: 'http://upload.qiniu.com/',
            allowedFileExtensions: ['jpg', 'png', 'gif', 'bmp'],
            maxFileSize: 1000,
            maxFilesNum: 1,
            uploadExtraData:{
                token : token

            },

        }).on('fileuploaded',function (event,data) {

            if(!data.response){
                layer.msg('上传失败！',{
                    icon : 1,
                    time :1000
                });
                return false;
            }

            $("#thumb").val(data.response.hash);
            token = ajax.getQiniuUploadToken();
        })

        $('.selectpicker').selectpicker({
            size: 'auto',
            noneSelectedText: '请选择'
        });

        $("#form-vertical").validate({
            highlight:function(r){
                $(r).closest(".form-group").addClass("has-error")
            },
            rules:{
                title:{
                    required : true
                },
                price:{
                    required : true
                },
                pay_price:{
                    required : true
                },
                description:{
                    required : true
                },
                content:{
                    required : function () {
                        var content = ue.getContent();
                        console.log(content);
                        return content;
                    }
                },
                coupon_url:{
                    required : true
                },
            },
            messages:{
                title:{
                    required : '标题不能为空'
                },
                price:{
                    required : '原价不能为空'
                },
                pay_price:{
                    required : '实际价格不能为空'
                },
                description:{
                    required : '请选择商品描述'
                },
                content:{
                    required : '请输入内容'
                },
                coupon_url:{
                    required : '请输入优惠券链接'
                }
            },
            unhighlight:function(r){
                $(r).closest(".form-group").removeClass("has-error")
            },
            errorElement:"span",
            errorClass:"help-block",
            errorPlacement:function(r,e){
                e.parent(".input-group").length?r.insertAfter(e.parent()):e.parent("label").length?r.insertBefore(e.parent()):r.insertAfter(e)
            },
            submitHandler: function (form) {
                Pace.restart();
                ajax.formSubmit('#form-vertical',function (data) {
                    if(data.err){
                        layer.msg(data.msg,{
                            icon:2,
                            time:1000
                        });
                        return false;
                    }
                    layer.msg(data.msg,{
                        icon:1,
                        time:1000
                    });
                    setTimeout(function () {
                        $('a[_href="{:url('admin/shop/index')}"]').click();
                    },1000);
                });
                return false;
            }
        });
    });
</script>